﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SummaryBackStageTabViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="SummaryBackStageTabViewModel" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Modules.Core.Dashboard.ViewModel
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Windows.Input;

    using Hisaab.Business.Interfaces;
    using Hisaab.Client.Common;
    using Hisaab.Client.Common.BaseClasses;
    using Hisaab.Client.Model;
    using Hisaab.Client.Modules.Core.Dashboard.Commands;
    using Hisaab.Common;

    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Defines the <seealso cref="SummaryBackStageTabViewModel"/> type.
    /// </summary>
    public class SummaryBackStageTabViewModel : ViewModelBase
    {
        /// <summary>
        /// Member variable _reportManager
        /// </summary>
        private readonly IReportsManager _reportManager;

        /// <summary>
        /// Member variable _reloadSummaryCommand
        /// </summary>
        private ICommand _reloadSummaryCommand;

        /// <summary>
        /// Member variable _summary
        /// </summary>
        private SummaryReport _summary;

        /// <summary>
        /// Initializes a new instance of the <see cref="SummaryBackStageTabViewModel"/> class.
        /// </summary>
        public SummaryBackStageTabViewModel()
            : this(ServiceLocator.Current.GetInstance<IReportsManager>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SummaryBackStageTabViewModel"/> class.
        /// </summary>
        /// <param name="reportManager">The report manager.</param>
        public SummaryBackStageTabViewModel(IReportsManager reportManager)
        {
            Contract.Requires<ArgumentNullException>(reportManager != null);

            _reportManager = reportManager;

            GlobalCommands.Refresh.RegisterCommand(ReloadSummaryCommand);

            ReloadSummary();
        }

        #region Command Properties

        /// <summary>
        /// Gets the reload summary command.
        /// </summary>
        /// <value>
        /// The reload summary command.
        /// </value>
        public ICommand ReloadSummaryCommand
        {
            get
            {
                if (_reloadSummaryCommand == null)
                {
                    _reloadSummaryCommand = new RefreshSummaryCommand(this);
                }

                return _reloadSummaryCommand;
            }
        }

        /// <summary>
        /// Gets or sets the summary report.
        /// </summary>
        /// <value>
        /// The summary report.
        /// </value>
        public SummaryReport Summary
        {
            get
            {
                return _summary;
            }

            set
            {
                RaisePropertyChangingEvent("Summary");
                _summary = value;
                RaisePropertyChangedEvent("Summary");
            }
        }

        #endregion Command Properties

        /// <summary>
        /// Reloads the summary.
        /// </summary>
        public void ReloadSummary()
        {
            Summary = _reportManager.GetGlobalSummary().Map<Business.Entities.SummaryReport, SummaryReport>();
        }
    }
}